package com.nb.cloud.doc.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lihaoyang
 * @date 2021/9/24
 */
@Configuration
public class RestClientConfig {


    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"));
        return new RestHighLevelClient(builder);
    }


//    @Bean
//    public RestHighLevelClient restHighLevelClient() {
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials(esUser, esPassword));
//        String[] hostArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(esHost, HOST_SPLIT);
//        HttpHost httpHost = new HttpHost(hostArr[1], 9200, hostArr[0]);
//        RestClientBuilder builder = RestClient.builder(httpHost)
//                .setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setKeepAliveStrategy((response, context) -> KEEP_ALIVE_MS))
//                .setRequestConfigCallback (new RestClientBuilder.RequestConfigCallback() {
//                    // 该方法接收一个RequestConfig.Builder对象，对该对象进行修改后然后返回。
//                    @Override
//                    public RequestConfig.Builder customizeRequestConfig(
//                            RequestConfig.Builder requestConfigBuilder) {
//                        return requestConfigBuilder.setConnectTimeout(5000 * 1000) // 连接超时（默认为1秒）
//                                .setSocketTimeout(6000 * 1000);// 套接字超时（默认为30秒）//更改客户端的超时限制默认30秒现在改为100*1000分钟
//                    }
//                });
//
//        return new RestHighLevelClient(builder);
//    }
}
